import java.util.Arrays;

public class MyPriorityQueue {

    public int[] elem;
    public int usedSize;

    public MyPriorityQueue(int[] elem) {
        this.elem = new int[10];
    }

    public void initElem(int[] array){
        for (int i = 0; i < array.length; i++) {
            elem[i] = array[i];
            usedSize++;
        }
    }

    public void createHeap(){
        for (int parent = (usedSize - 1) / 2; parent >= 0; parent--){
            siftDown(parent,usedSize);
        }
    }

    public void siftDown(int parent,int usedSize){
        int child = parent*2 + 1;
        while(child < usedSize){
            if(child + 1 < usedSize){
                if(elem[child] > elem[child + 1]){
                    child++;
                }
            }

            if(elem[child] > elem[parent]){
                break;
            }
            swap(elem,parent,child);
            parent = child;
            child = parent*2 + 1;
        }
    }
    private void swap(int[] elem,int parent,int child){
        int tmp = elem[child];
        elem[child] = elem[parent];
        elem[parent] = tmp;
    }

    public void offer(int val){
        if(isFull()){
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        usedSize++;
        elem[usedSize - 1] = val;
        int child = usedSize - 1;
        int parent = (child - 1)/2;
        while(parent >= 0){
            if(elem[child] >= elem[parent]){
                break;
            }else{
                swap(this.elem,parent,child);
                child = parent;
                parent = (child - 1)/2;
            }
        }
    }

    private boolean isFull(){
        return usedSize == elem.length;
    }


    public int poll(){
        if(isEmpty()){
            return -1;
        }
        int val = elem[0];
        swap(elem,0,usedSize - 1);
        usedSize--;
        siftDown(0,usedSize);
        return val;
    }

    private boolean isEmpty(){
        return usedSize == 0;
    }

    public int peek(){
        if(isEmpty()){
            return -1;
        }
        return elem[0];
    }


}

